﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace FloatFall
{
    public partial class Form1 : Form
    {
        private const string C_DIR_IMGS = "res";

        int _snowNum = 10;
        bool _isPlayMusic = false;
        string[] _imgs = null;
        List<Snow> _snowList = new List<Snow>();

        public Form1()
        {
            InitializeComponent();
            this.TransparencyKey = this.BackColor;

            _snowNum = Convert.ToInt32(ConfigurationManager.AppSettings["snow_num"]);
            _isPlayMusic = Convert.ToBoolean(ConfigurationManager.AppSettings["play_music"]);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            if (!Directory.Exists(C_DIR_IMGS))
            {
                MessageBox.Show("Images文件夹不存在。");
                return;
            }
            var pattern = "\\.(jpg|gif|png)";
            _imgs = Directory.GetFiles(C_DIR_IMGS).Where(f => Regex.IsMatch(f.ToLower(), pattern)).ToArray();
            if (_imgs.Length == 0)
            {
                MessageBox.Show("没有图片素材。");
                return;
            }

            var rnd = new Random(DateTime.Now.Millisecond);
            for (int i = 0; i < _snowNum; i++)
            {
                var imgFile = _imgs[rnd.Next(0, _imgs.Length)];
                _snowList.Add(new Snow { Image = Image.FromFile(imgFile) });
            }

            // 采用双缓冲
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.AllPaintingInWmPaint, true);
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            tmrFlow.Start();
            tmrChangeStyle.Start();

            var isPlayMusic = Convert.ToBoolean(ConfigurationManager.AppSettings["play_music"]);
            var musicPath = ConfigurationManager.AppSettings["music_path"];
            if (isPlayMusic && File.Exists(musicPath))
            {
                // 好像只能播Mp3
                MusicBox.Instance.Play(musicPath);
            }
        }

        private void tsmiExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void tsmiAbout_Click(object sender, EventArgs e)
        {
            Process.Start("https://gitee.com/yokeqi/FloatFall");
        }

        private void tmrFPS_Tick(object sender, EventArgs e)
        {
            _snowList.ForEach(s => s.Float());

            Invalidate();
        }

        private void tmrChangeStyle_Tick(object sender, EventArgs e)
        {
            _snowList.ForEach(s => s.ChagneStyle());
            Invalidate();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            if (_imgs == null || _imgs.Length == 0)
                return;

            e.Graphics.Clear(this.BackColor);

            foreach (var snow in _snowList)
            {
                e.Graphics.DrawImage(snow.Image, snow.CurPos);
            }
        }
    }
}
